#pragma once
#include "stdafx.h"
#include "resource.h"
extern  xmpp_mgr	*_pxmpp_client;
class CLoginDlg : public CAxDialogImpl<CLoginDlg>,public CUpdateUI<CLoginDlg>,public CMessageFilter,public CIdleHandler
{
private:
	bool		blogined;
	CComboBox	m_editUser;
	CEdit		m_editPass;

public:
	enum { IDD = IDD_DIALOG_LOGIN };
	bool		m_bShow;
	virtual BOOL PreTranslateMessage(MSG *pMSG)
	{
		if(m_hWnd)
			return CWindow::IsDialogMessage(pMSG);
		return false;
	}
	virtual BOOL OnIdle()
	{
		return false;
	}

	BEGIN_UPDATE_UI_MAP(CLoginDlg)
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP(CLoginDlg)
		MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
		COMMAND_ID_HANDLER(IDOK, OnStartLogin)
		COMMAND_ID_HANDLER(IDCANCEL, OnCloseCmd)
		CHAIN_MSG_MAP(CUpdateUI<CLoginDlg>)
	END_MSG_MAP()

	
	// Handler prototypes (uncomment arguments if needed):
	//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
	//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

		LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
		{
			CMessageLoop* pLoop = _Module.GetMessageLoop();
			ATLASSERT(pLoop != NULL);
			pLoop->AddMessageFilter(this);
			pLoop->AddIdleHandler(this);

			blogined = false;
			CenterWindow(GetParent());
			//ModifyStyleEx(0,WS_EX_APPWINDOW); 
			UIAddChildWindowContainer(m_hWnd);
			m_editUser.Attach(GetDlgItem(IDC_COMBO_USER));
			m_editPass.Attach(GetDlgItem(IDC_EDIT_PASS));
			m_editUser.SetWindowText(L"user1");
			m_editPass.SetWindowText(L"123456");
			m_bShow = true;
			return TRUE;
		}
		LRESULT OnCloseCmd(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
		{
			CloseDialog(wID);
			if (wID==IDCANCEL)
			{
				_pxmpp_client->exit();
			}
			return 0;
		}

		LRESULT OnStartLogin(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
		{
			CreateThread(NULL,NULL,LoginThread,this,NULL,NULL);
			return 0;
		}
		void CloseDialog(int nVal)
		{
			DestroyWindow();
			::PostQuitMessage(nVal);
			
		}
public:
	static DWORD WINAPI LoginThread(LPVOID	lp)
	{
		CLoginDlg *self = (CLoginDlg *)lp;
		wchar_t	str[128];
		load_config_string(L"network",L"xmpp_server",L"192.168.3.208",str,128);
		wchar_t pass[128];
		wchar_t user[128];
		self->m_editPass.GetWindowText(pass,128);
		self->m_editUser.GetWindowText(user,128);
		if(_pxmpp_client->login(wstring2string(wstring(user)).c_str(),wstring2string(wstring(pass)).c_str(), wstring2string(wstring(str)).c_str(),""))
		self->CloseLogin();
		return NULL;
	}
public:	
	bool CloseLogin()
	{
		PostMessage(WM_CLOSE);
		return true;
	}
	bool ShowMsg(string msg)
	{
		GetDlgItem(IDC_STATIC_TEXT).SetWindowText(string2wstring(msg).c_str());
		return true;
	}
};